package com.yz.smartpos.yztester.util;

import android.content.Context;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.yz.smartpos.yztester.ui.MainActivity;
import com.yz.smartpos.yztester.ui.OperateFragment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * Created by zhuxy on 2017/9/13.
 */

public class TesterConfig {
    public static String TAG = "TesterConfig";

    public static String TESTER_PATH = "";
    public static String CONFIG_PATH = "";
    public static String RESULTS_PATH = "";
    public static String TESTSUITES_PATH = "";


    public static String result_file_suc = "";
    public static String result_file_fail = "";
    public static String result_file_unsport = "";
    public static String result_file_runned = "";
    public static String result_file_log = "";
    public static FileWriter result_log_out;
    public static FileWriter caserunned_out;
    public static String result_pm_log = "";
    public static FileWriter result_pm_out;

    public static String PN = "";
    public static String InnerVersion = "";
    public static String CustomVersion = "";
    public static String SoftVersion = "";
    public static String sdkVerCode = "";
    public static String sdkVerName = "";

    private static void readTestcaseFromFile(String result_file, Set<TestCase> caseList) {
        try {
            char[] buf = new char[1024];
            FileInputStream fis = new FileInputStream(result_file);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            Log.i(TAG, "result_file:" + result_file);

            String line = null;
            Map<String, String[]> tmp_list = new HashMap();
            while ((line = br.readLine()) != null) {
                Log.i(TAG, "line:" + line);
                String[] tmp = line.split(",");

                if (tmp.length != 4 && tmp.length != 5 && tmp.length != 6)
                    continue;
                tmp_list.put(tmp[3], tmp);
            }
            for(String[] tmp : tmp_list.values()){
                TestCase inter = TesterState.dataSet.get(TesterState.interfaceList.get(Integer.valueOf(tmp[0]))).get(Integer.valueOf(tmp[1]));
                TestCase tmpTescase = null;
                Log.i(TAG, "testcase:" + inter.getCaseName());
                if (inter.getCaseName().equals(tmp[3])) {
                    tmpTescase = inter;
                } else {
                    for (TestCase testcase : TesterState.dataSet.get(TesterState.interfaceList.get(Integer.valueOf(tmp[0])))) {
                        if (testcase.getCaseName().equals(tmp[3])) {
                            tmpTescase = testcase;
                            break;
                        }
                    }
                }
                if (tmpTescase != null) {
                    Log.i(TAG, String.format("appendcacse:%s index:%d", tmpTescase.getCaseName(), tmpTescase.index1));
                    caseList.add(tmpTescase);
                    tmpTescase.setmResultState(tmp[4]);
                    if(tmp.length == 6)
                        tmpTescase.lastestRuntime = tmp[5];
                    if(tmpTescase.getmResultState() == TestCase.STATUS_PASSED)
                        TesterState.caseList_suc.add(tmpTescase);
                    else if(tmpTescase.getmResultState() == TestCase.STATUS_FAILED)
                        TesterState.caseList_fail.add(tmpTescase);

                } else {
                    Log.e(TAG, "Case not found testcase,pls check:" + tmp.toString());
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    public static void initDir(Context context) {
        File[] filePath = ContextCompat.getExternalFilesDirs(context, null);
        File cache = context.getCodeCacheDir();

        TESTER_PATH = filePath[0].getPath() + File.separator;

        File file = new File(TESTER_PATH);
        if (!file.exists()) {
            file.mkdirs();
            Utils.chmod777Path(TESTER_PATH);
        }

        //init all paths
        CONFIG_PATH = TESTER_PATH + "Config/";
        RESULTS_PATH = TESTER_PATH + "Results/";  //

        result_file_suc = RESULTS_PATH + "/case_succes.txt";
        result_file_fail = RESULTS_PATH + "/case_fail.txt";
        result_file_unsport = RESULTS_PATH + "/case_unsport.txt";
        result_file_runned = RESULTS_PATH + "/case_runned.txt";

        result_file_log = RESULTS_PATH + "/test.log";
        result_pm_log = RESULTS_PATH + "/test_pm.log";

        Log.i(TAG, "in initDir");
        file = new File(RESULTS_PATH);
        if (!file.exists()) {
            file.mkdirs();
            Utils.chmod777Path(RESULTS_PATH);
        }

        file = new File(CONFIG_PATH);
        if (!file.exists()) {
            file.mkdirs();
            Utils.chmod777Path(CONFIG_PATH);
        }
        Utils.chmod777Path(TESTER_PATH);

        try {
            file = new File(result_file_log);
            if (!file.exists()) {
                file.createNewFile();
                Utils.chmod777Path(result_file_log);
            }
            result_log_out = new FileWriter(result_file_log, true);

            file = new File(result_pm_log);
            if (!file.exists()) {
                file.createNewFile();
                Utils.chmod777Path(result_pm_log);
            }
            result_pm_out = new FileWriter(result_pm_log, true);

            file = new File(result_file_runned);
            if (!file.exists()) {
                file.createNewFile();
                Utils.chmod777Path(result_file_runned);
            } else if (file.length() > 10) {
                readTestcaseFromFile(result_file_runned, TesterState.caseList_runned);
            }

            Log.i(TAG, "in initDir33");
            TesterConfig.getVerion();
        } catch (IOException e) {
            Log.i("tmp", e.getMessage());
        } catch (Exception e) {
            Log.i("tmp", e.getMessage());
        }

    }

    public static void getVerion() {
//        DeviceInformation deviceInformation = smartPosApi.getDeviceInformation();
//        TesterConfig.PN = deviceInformation.getDeviceInfo().getDeviceModel();
//        TesterConfig.InnerVersion = deviceInformation.getDeviceVersion().getInnerVersion();
//        TesterConfig.CustomVersion = deviceInformation.getDeviceVersion().getCustomVersion();
//        TesterConfig.SoftVersion = deviceInformation.getDeviceVersion().getSoftVersion();
//        Log.i(TAG, "InnerVersion:" + TesterConfig.InnerVersion);
//        Log.i(TAG, "CustomVersion:" + TesterConfig.CustomVersion);
//        Log.i(TAG, "SoftVersion:" + TesterConfig.SoftVersion);
    }

    public static void writeCaseRunned(TestCase testcase) {
        Log.i(TAG, "writeCaseRunned:" + testcase.getCaseName());
        String str = "" + testcase.index1 + "," + testcase.index2 + "," + testcase.index3 + "," + testcase.getCaseName() + "," + testcase.getmResultState() + "," + testcase.lastestRuntime +"\n";
        try {
            caserunned_out = new FileWriter(result_file_runned, true);
            caserunned_out.append(str);
            caserunned_out.flush();
        } catch (IOException e) {
            Log.i("tmp", e.getMessage());
        }
    }

    public static void tearDir() {
        Log.i(TAG, "tearDir caseList_runned len:" + TesterState.caseList_runned.size());

        try {
            caserunned_out = new FileWriter(result_file_runned, true);
            String str;
            for (TestCase testcase : TesterState.caseList_runned) {
                str = "" + testcase.index1 + "," + testcase.index2 + "," + testcase.index3 + "," + testcase.getCaseName() + "," + testcase.getmResultState() + "," + testcase.lastestRuntime +"\n";
                caserunned_out.append(str);
            }

//            result_log_out.flush();
//            result_pm_out.flush();
            caserunned_out.flush();
        } catch (IOException e) {
            Log.i("tmp", e.getMessage());
        }
    }

}
